<?php

//This is the file I gave you earlier with the database connection script and stuff.
require_once("overhead.php");

class Department implements DataObject{

	var $ID; //corresponds to the Primary key in the department table (also called ID)
	var $code; //Corresponds to the department prefix code (ie: "CS","LCC",etc.)
	var $name; //Full department name.
	
	
	//Getters and setters don't directly touch the database.
	public function getID() {
		return $this->ID;
	}
	public function getCode() {
		return $this->code;
	}
	public function getName() {
		return $this->name;
	}
	public function setCode($c) {
		$this->code=$c;
	}
	public function setName($n) {
		$this->name=$n;
	}
	
	public function retrieveFromCode($c) {
		$queryString="select ID, Code, Name from Department where Department.Code='".$c."'";
		$query=mysql_query($queryString);
		$result=mysql_fetch_assoc($query);
		 if ($result) {
		 	//If something was found, put all the fields from the database (in $result right now) into
		 	//the instance variables for this object.
 			$this->ID=$result['ID'];
 			$this->code=$result['Code'];
 			$this->name=$result['Name'];
 			//return true because we found it!
 			return true;
 		} else {
 			//it wasn't found or there was an error, so return false.
 			return false;
 		}
	}
	
	public function retrieveFromID($i) { //Get Department info from the database for a department with Department.ID=$i
		$queryString="select ID, Code, Name from Department where Department.ID=".$i;
		$query=mysql_query($queryString);
		$result=mysql_fetch_assoc($query);
		 if ($result) {
		 	//If something was found, put all the fields from the database (in $result right now) into
		 	//the instance variables for this object.
 			$this->ID=$result['ID'];
 			$this->code=$result['Code'];
 			$this->name=$result['Name'];
 			//return true because we found it!
 			return true;
 		} else {
 			//it wasn't found or there was an error, so return false.
 			return false;
 		}
	}
	
	public function update() { //Update the contents of the database to reflect any changes made to this object.
		$queryString="update Department set Code='".$this->code."', Name='".$this->name."' where Department.ID=".$this->ID;
		if (mysql_query($queryString)) { //if the query fails, then $query will be NULL or false. since we are not
										 //doing anything with any resulting rows, we don't have to create another variable
										 //for the result of mysql_query().
			return true; //Successfully updated!
		} else {
			return false; //There was a problem with the UPDATE query!
		}
	}
	public function insert() { //This takes all the values of the instance variables and creates a NEW entry in the 
							   //Department table for these values.
		$queryString="insert into Department (Code, Name) values ('".$this->code."','".$this->name."')";
		if (mysql_query($queryString)) { //if the insert was successful, this will not be null or false.
			//BUT! we have one more step. if you check the spefications, it says we have to update the value
			//of $this->ID to hold the ID that the database created (with auto_increment) for this new department.
			//We can do that by selecting LAST_INSERT_ID().
			$getIDQueryString="select LAST_INSERT_ID() as NewID";
			$getIDQuery=mysql_query($getIDQueryString);
			$result=mysql_fetch_assoc($getIDQuery);
			if ($result) {
				$this->ID=$result['NewID'];
				return true;
			} else { return false; }
		} else {
			return false;
		}
	}
	public function delete() { //This deletes the row of the Department table that has and ID corresponding
							   //to $this->ID
		$queryString="delete from Department where Department.ID=".$this->ID;
		if (mysql_query($queryString)) {
				return true;
		}
		else {
				return false;
		}
		
	}
	public function getLatest() {
                $queryString="select ID, Code, Name from Department where Department.ID=".$this->ID;
                $query=mysql_query($queryString);
                $result=mysql_fetch_assoc($query);
                 if ($result) {
                        //If something was found, put all the fields from the database (in $result right now) into
                        //the instance variables for this object.
                        $this->ID=$result['ID'];
                        $this->code=$result['Code'];
                        $this->name=$result['Name'];
                        //return true because we found it!
                        return true;
                } else {
                        //it wasn't found or there was an error, so return false.
                        return false;
                }
        }
		
}

?>
